﻿using System;
using System.Collections.Generic;
using System.Security.Permissions;

namespace TaskKernel.Domain.Services.Action
{
    public class TaskAction : ITaskAction
    {
        private readonly FileManager _fileManager;

        public TaskAction(FileManager fileManager)
        {
            _fileManager = fileManager;
        }

        #region ITaskAction Members

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage")]
        public virtual bool ForceCloseTask(Task task, Account account)
        {
            return task.ForceClose(account);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor, Owner")]
        public virtual bool ChangeTaskName(Task task, string taskName, Account currentAccount)
        {
            return task.ChangeTaskName(taskName, currentAccount);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor")]
        public virtual bool SetExecutor(Task task, Account executor, Account logonAccount)
        {
            return task.SetExecutor(executor, logonAccount);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor")]
        public virtual bool SetPercentComplete(Task task, int percentComplete, Account logonAccount)
        {
            return task.SetPercentComplete(logonAccount, percentComplete);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor")]
        public virtual bool RequestCloseTask(Task task, Account logonAccount)
        {
            return task.RequestClose(logonAccount);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner")]
        public virtual bool StartWorkingPeriodTrigger(Task task, Account currentAccount)
        {
            return task.StartWorkingPeriodTrigger(currentAccount);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner")]
        public virtual bool SetExpirationDate(Task task, DateTime? date, Account logonAccount)
        {
            return task.SetExpirationDate(date, logonAccount);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage,Executor,Owner")]
        public virtual bool SetDepartment(Task task, Department department, Account logonAccount)
        {
            return task.SetDepartment(logonAccount, department);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage,Executor,Owner")]
        public virtual bool UnCloseTask(Task task, Account logonAccount)
        {
            return task.UnClose(logonAccount);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner")]
        public virtual bool CloseTask(Task task, Account logonAccount)
        {
            return task.Close(logonAccount);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner,Participant")]
        public virtual bool DeleteParticipant(Task task, IEnumerable<Account> participant, Account logonAccount)
        {
            return task.DeleteParticipants(logonAccount, participant);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner,Participant")]
        public virtual bool AddParticipant(Task task, IEnumerable<Account> participants, Account logonAccount)
        {
            return task.AddParticipants(logonAccount, participants);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner,Participant")]
        public virtual bool ViewTask(Task task, Account logonAccount)
        {
            task.ViewTask(logonAccount);
            return true;
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner,Participant")]
        public virtual bool AddComment(Task task, string comment, Account logonAccount)
        {
            return task.AddHistoryEntry(logonAccount, comment);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner,Participant")]
        public virtual bool SetTag(Task task, string tag, Account logonAccount)
        {
            return task.SetTag(tag, logonAccount);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner,Participant")]
        public virtual bool AddFile(Task task, File file, Account logonAccount)
        {
            if (task.AddFile(logonAccount, file))
                return _fileManager.SaveToFileSystem(Convert.ToString(task.UID), task.Code, file.FileName, Convert.ToString(file.Hash), file.Content);
            return false;
        }

        public virtual IEnumerable<File> LoadFiles(Task task)
        {
            foreach (var file in task.Files){
                var content = _fileManager.LoadFromFileSystem(Convert.ToString(task.UID), task.Code, file.FileName, Convert.ToString(file.Hash));
                if (content.Length <= 0) continue;
                file.SetContent(content);
                yield return file;
            }
        }

        public void SetRating(Task task, Account currentAccount, int value, string text, string role)
        {
            if (task.HasRatingToChanging(currentAccount, new TimeSpan(14, 0, 0, 0))) task.ChangeRating(currentAccount, value, text, role);
            else task.AddRating(currentAccount, value, text, role);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner,Participant")]
        public virtual bool AddReminder(Task task, DateTime datetime, string note, Account logonAccount)
        {
            return task.AddReminder(logonAccount, datetime, note);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner,Participant")]
        public virtual bool DeleteReminder(Task task, Account logonAccount)
        {
            return task.DeleteReminder(logonAccount);
        }

        [PrincipalPermission(SecurityAction.Demand, Role = "Admin, AdminWithoutMessage, Executor,Owner,Participant,Other,Anonymous")]
        public virtual bool SetPriority(Task task, TaskPriority priority)
        {
            return task.SetPriority(priority);
        }

        #endregion
    }
}